package com.lagou.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lagou.mapper.ArticleMapper;
import com.lagou.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @Title: blog
 * @Description:
 * @Author: Wenfeng Cheng
 * @Date: 2021-04-05 14:57
 * @Version: 1.0.0
 */
@Controller
public class ArticleController {

    @Autowired
    private ArticleMapper articleMapper;

    @RequestMapping(value = "/client/index",method = RequestMethod.GET)
    public String index(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                        @RequestParam(value = "pageSize", defaultValue = "2") Integer pageSize,
                        Model model){
        if(pageNum <= 0){
            pageNum = 1;
        }
        if(pageSize <= 0){
            pageSize = 1;
        }
        System.out.println("当前页是："+pageNum+" 显示条数是："+pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<Article> articles = articleMapper.selectAll();
        System.out.println("分页数据："+articles);

        PageInfo<Article> pageInfo = new PageInfo<>(articles,pageSize);

        model.addAttribute("articles",articles);
        model.addAttribute("pageInfo", pageInfo);
        return "/client/index";
    }
}
